import vm from '@/main'
import Vue from 'vue'
import AlertComponent from './common-confirm.vue'

const AlertConstructor = Vue.extend(AlertComponent)

let dialog = undefined

const Confirm = (options = {}) => {
  const opts = {
    title: '',
    loading: false,
    cancelButtonType: '',
    cancelText: vm.$t('common.common_confirm.button.cancel'),
    ...options,
  }

  if (dialog) {
    return dialog
  }

  const parent = document.body

  const instance = new AlertConstructor({
    el: document.createElement('div'),
    data: opts
  })

  parent.appendChild(instance.$el)

  Vue.nextTick(() => {
    instance.dialogVisible = true
  })

  instance.close = () => {
    instance.dialogVisible = false
    parent.removeChild(instance.$el)
    dialog = undefined
  }

  dialog = instance

  return instance
}

export default Confirm
